//
//  ViewController.swift
//  SlideDeleteBtnDemo
//
//  Created by cgw on 2021/3/22.
//  Copyright © 2021 shenyi. All rights reserved.
//  Git: https://gitee.com/dosedo/modify-delete-btn-demo
//

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    let tableView = UITableView()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let size = self.view.frame.size
        tableView.frame = .init(x: 0, y: 100, width: size.width, height: size.height-100)
        tableView.rowHeight = 100;
        tableView.delegate = self
        tableView.dataSource = self
        tableView.allowsSelection = false
        tableView.separatorStyle = .none
        self.view.addSubview(tableView)
        tableView.reloadData()
    }
    
    // MARK: - 左滑删除

    ///iOS11以下，使用此方法
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        
        ///此时调用是为了防止：当结束编辑时，删除按钮还未完全消失时，
        ///再次左滑，修改会失效，所以再次修改下
        self.setupDeleteBtnFrame(indexPath: indexPath)
        
        let deleteAction = UITableViewRowAction.init(style: .default, title: "删除") { (aciton, indexPath) in
            print("删除完成")
            ///点击删除后，结束编辑
            tableView.setEditing(false, animated: true)
            
        }
        return [deleteAction]
    }

    ///iOS11及以上使用此方法
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        
        ///此时调用是为了防止：当结束编辑时，删除按钮还未完全消失时，
        ///再次左滑，修改会失效，所以再次修改下
        self.setupDeleteBtnFrame(indexPath: indexPath)
        
        func handleDelete(action: UIContextualAction, view: UIView, endEditing:@escaping (Bool)->Void){
            print("删除成功")

            ///点击删除后，结束编辑
            endEditing(true)
        }

        let handle : UIContextualAction.Handler = handleDelete(action:view:endEditing:)
        let deleteAction = UIContextualAction.init(style: .destructive, title: "删除", handler: handle)
        let conf = UISwipeActionsConfiguration.init(actions: [deleteAction])
        
        return conf
    }
    
    ///修改删除按钮大小
    func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
        ///当即将开始编辑，修改删除按钮的大小
        self.setupDeleteBtnFrame(indexPath: indexPath)
    }
    
    func setupDeleteBtnFrame(indexPath: IndexPath){
        self.tableView.setupSlideDeleteBtnOriginY(10, height: 90, editing: indexPath)
    }
    
    // MARK: - UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 30
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let reuseId = "cellid"
        var cell = tableView.dequeueReusableCell(withIdentifier: reuseId)
        
        if cell == nil {
            cell = UITableViewCell.init(style: .default, reuseIdentifier: reuseId)
            cell?.backgroundColor = .white
            
            let bgColor = UIColor.init(red: CGFloat(arc4random()%255)/255.0, green: CGFloat(arc4random()%255)/255.0, blue: CGFloat(arc4random()%255)/255.0, alpha: 1)
            
            let textL = UILabel()
            textL.backgroundColor = bgColor
            textL.font = UIFont.boldSystemFont(ofSize: 30)
            textL.textColor = .white
            textL.textAlignment = .center
            textL.frame = .init(x: 0, y: 10, width: self.view.frame.width, height: 90)
            textL.tag = 100
            cell?.contentView.addSubview(textL)
        }

        if let t = cell?.contentView.viewWithTag(100) as? UILabel {
            t.text = "\(indexPath.row+1)"
        }
        return cell!
    }
}

